package pirpc

import (
	"errors"
	"net/rpc"

	"gitee.com/dayu1985/framework/context"
)

type PiRpcClient struct {
	addr string
}

func NewPiRpcClient(config *context.Configuration) (*PiRpcClient, error) {

	cli := PiRpcClient{
		addr: config.INI.MustValue("szsw", "pirpc.addr", "127.0.0.1:1234"),
	}

	return &cli, nil
}

func (this *PiRpcClient) Call(serviceName string, args interface{}, reply interface{}) error {

	client, err := rpc.DialHTTP("tcp", this.addr)

	if err != nil {
		return errors.New("无法与PI-SERVER连接")
	}

	defer client.Close()

	return client.Call(serviceName, args, reply)
}
